using GEM.Domain.Events;

namespace GEM.Domain.Entities;

public abstract class EntityBase
{
    public Guid Id { get; set; }

    //为实体对象分配一个唯一的编码或编号
    public string? Code { get; set; }

    //是否激活状态
    public bool IsActived { get; set; } = true;

    //标记实体对象是否被逻辑删除
    public bool IsDeleted { get; set; } = false;

    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
    public Guid CretedBy { get; set; } = Guid.Empty;
    public Guid UpdatedBy { get; set; } = Guid.Empty;

    //用于实体对象的显示排序
    public int DisplayOrder { get; set; }

    public string? Remarks { get; set; } = string.Empty;


    /// <summary>
    /// 用于临时存储当前对象（实体/聚合根）产生的领域事件
    /// </summary>
    private readonly List<IDomainEvent> _domainEvents = new List<IDomainEvent>();

    /// <summary>
    /// 获取当前对象（实体/聚合根）产生的领域事件
    /// </summary>
    public IReadOnlyCollection<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();

    /// <summary>
    /// 添加领域事件
    /// </summary>
    protected void AddDomainEvent(IDomainEvent domainEvent)
    {
        _domainEvents.Add(domainEvent);
    }

    /// <summary>
    /// 移除领域事件
    /// </summary>
    public void RemoveDomainEvent(IDomainEvent domainEvent)
    {
        _domainEvents.Remove(domainEvent);
    }

    /// <summary>
    /// 清空所有领域事件
    /// </summary>
    public void ClearDomainEvents()
    {
        _domainEvents.Clear();
    }

}